﻿#include "WidgetReszimg.h"

#include <QHBoxLayout>
#include <QImageReader>
#include"opencv.hpp"
WidgetReszImg::WidgetReszImg(QWidget *parent) : QWidget(parent)
{
  setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  image_label->setBackgroundRole(QPalette::Base);
  image_label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  image_label->setScaledContents(true);//使图片能缩放铺满整个label
}

//这个把参数可以自行改成QImage
void WidgetReszImg::setImg(cv::Mat img)
{
  QImage image;
  image = QImage(reinterpret_cast<unsigned char*>(img.data),
                 img.cols,
                 img.rows,
                 static_cast<int>(img.step),
                 QImage::Format_Grayscale8).copy();
  image_label->setPixmap(QPixmap::fromImage(image));
}

void WidgetReszImg::resizeEvent(QResizeEvent *event)
{
  float scale = std::min( (float)width()/2592.0,(float)height()/2048.0);
  image_label->resize(floor(2592*scale),floor(2048.0*scale));

}
